PHP voorbeeldcode

Bijlage 1: Voorbeeldcode PHP (Orgineel: Binaries -> ShopServer.php)

Sla de volgende code op als ShopServer.php of een naam naar keuze of kopieer ShopServer.php uit de binaries van deze QA.

<?php
class RetailVista_ShopServer{
 public $Client, $Debug, $Version;
 
 private static $_instance;
 
 /**
  * Service connection info
  * @var array
  */
 private static $_connectionInfo; 
 
 public static function getInstance() {
  if (!isset(self::$_instance)) {
   if (!isset(self::$_connectionInfo)) {
    throw new Bp_Exception("No Retail Vista connection info provided.");
   }
   self::$_instance = new RetailVista_ShopServer(self::$_connectionInfo);
  }
  
  return self::$_instance;
 }
 
 public static function resetInstance() {
  self::$_instance = null;
 }
 
 /**
  * Webservice connection info
  *
  * @param array $info
  * @param bool $suppressException
  */
 public static function setConnectionInfo(array $info, $suppressException = false) {

  $requiredFields = array('sShopServerUsername', 'sShopServerPassword', 'sShopServerStoreNumber', 'sShopServerURL');
  
  if (!is_array($info)) {
   throw new Bp_Exception('Invalid argument: need array');
  }
  
  // validate required fields
  foreach($requiredFields AS $field) {
   if (!array_key_exists($field, $info) || empty($info[$field]) && !$suppressException) {
    throw new Bp_Exception('Invalid argument: field ' . $field . ' is required and can\'t be empty.');
   }
  }
  static::$_connectionInfo = $info;
 }
 
 private function __construct($aParams = array())
 {
  // Reset cache
  if($aParams['sShopServerResetWSDL'] == true) ini_set('soap.wsdl_cache_ttl', 1);
  
  $username   = 'name='.$aParams['sShopServerUsername'].';storenumber='.$aParams['sShopServerStoreNumber'].';';
  if($aParams['sShopServerSubStoreNumber']!=''){
   $username .= 'substorenumber='.$aParams['sShopServerSubStoreNumber'].';';
  }
  $password   = $aParams['sShopServerPassword'];
  $url    = $aParams['sShopServerURL'];
  
  $strWsSecurityNs  = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
  
  $objUserPass = new WsSecurityUsernamePassword(
   new SoapVar($username, XSD_STRING, NULL, $strWsSecurityNs , NULL, $strWsSecurityNs), /*username*/
   new SoapVar($password, XSD_STRING, NULL, $strWsSecurityNs , NULL, $strWsSecurityNs) /*password*/
  );

  $objSoapWsUsernameToken = new SoapVar(
   new WsUsernameToken(new SoapVar($objUserPass, SOAP_ENC_OBJECT, NULL, $strWsSecurityNs , 'UsernameToken', $strWsSecurityNs)),
   SOAP_ENC_OBJECT,
   NULL,
   $strWsSecurityNs,
   'UsernameToken',
   $strWsSecurityNs
  );

  $objSoapWsSecurityHeader = new SoapHeader(
   $strWsSecurityNs,
   'Security',
   new SoapVar($objSoapWsUsernameToken, SOAP_ENC_OBJECT, NULL, $strWsSecurityNs , 'Security', $strWsSecurityNs),
   true // Must Understand
  );
  
  //retailvista_shopserver/webservice/version
  $svc = 'ShopServerService.svc';
  if(!empty($aParams['sShopServerVersion']) && strlen($aParams['sShopServerVersion'])==6){
   $svc = 'ShopServerService'.$aParams['sShopServerVersion'].'.svc';
  }
  
  $this->Client = new SoapClient(
   $url.'/'.$svc.'?wsdl', /* Realtime WSDL Location. It is preferred to use the downloaded WSDL, according to the documentation.*/
   array( 
    'soap_version' => SOAP_1_2,
    'features'  => SOAP_SINGLE_ELEMENT_ARRAYS,
    'location'  => $url.'/'.$svc.'/soap12', /*Service location*/
    'uri'   => 'http://services.nedfox.net/shopserver' /*Service targetnamespace*/
       )
  );

  $this->Client->__setSoapHeaders(array($objSoapWsSecurityHeader));
  $this->Version = $aParams['sShopServerVersion'];
  $this->Debug = $aParams['sShopServerDebug'];
 }

 /*
  * Magic Method
  *
  */
 public function __call($sMethod, $aArguments)
 {
  // Maak de call.
  ini_set('memory_limit', '1280M');
  $Result = $this->Client->$sMethod(new NedFoxShopServerRequest($aArguments[0]));
  $sResultMethod = $sMethod.'Result';
  
  // Laat debug info zien van wat er gecalled wordt.
  if($this->Debug == true)
  {
   echo '<span style="color: #777;">';
   echo 'Called method: '.$sMethod;
   
   if(!empty($aArguments))
    echo ', with arguments: <pre>'.print_r($aArguments[0],true).'</pre>';
   
   if(!empty($Result))
    echo '<br />Result: <pre>'.print_r($Result,true).'</pre>';
   
   echo '</span><br />';
  }
  
  // Geef het resultaat terug.
  return $Result->$sResultMethod->Value;
 }


 
 /*
  * Base64 en utf-32 decode het bericht voor communicatie met de ShopServer WebService.
  */
 public static function Decode($sString){
  return iconv('UTF-32','UTF-8',base64_decode($sString));
 }

}

// Communicatie classes.
class WsSecurityUsernamePassword
{
 private $Username, $Password;
 function __construct($username, $password)
 {
  $this->Username = $username;
  $this->Password = $password;
 }
}
class WsUsernameToken
{
 private $UsernameToken;
 function __construct($innerVal)
 {
  $this->UsernameToken = $innerVal;
 }
}
class NedFoxShopServerRequest
{
 public $request;
 function __construct($payload)
 {
  $this->request = $payload;
 }
}

?>

Bijlage 2: Gebruik ShopServer.php

Zie evt. ook het bestand test.php in de binaries.

//include the Shopserver class, depending on how you called it (fe. ShopServer.php).

// connect to retailvista shopserver
RetailVista_ShopServer::setConnectionInfo(array(
 'sShopServerUsername'  => 'username',
 'sShopServerResetWSDL'  => false,
 'sShopServerPassword'  => 'password',
 'sShopServerStoreNumber'  => 'storenumber',
 'sShopServerDebug'    => false,
 'sShopServerURL'   => 'url', => //example: "https://shopserver.nedfox.net"
 'sShopServerVersion'  => 'version' // can be empty, example: "_201502"
), false);

// create Shopserver object
$Shopserver = RetailVista_ShopServer::getInstance();

// after this you can call the functions directly, for example how to call the
$filterBrand = $Shopserver->NewBrandsFilter();
$resultBrands = $Shopserver->GetBrands(array('Filter' =>$filterBrand));